package com.cxyzy.basic_libs.anim

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.util.AttributeSet
import android.view.View
import android.view.animation.Animation
import android.view.animation.ScaleAnimation
import android.widget.RelativeLayout

class CircleScale(context: Context, attributeSet: AttributeSet? = null) :
    RelativeLayout(context, attributeSet) {

    init {
        val view = View(context)
        view.background = getCircle("#fab27b")
        val layoutParams = LayoutParams(200, 200)
        layoutParams.addRule(CENTER_IN_PARENT)
        addView(view, layoutParams)
        scaleDown(this)
    }

    private fun scaleDown(srcView: View) {
        val animateTime = 2000L
        val animation = ScaleAnimation(1f, 2f, 1f, 2f,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f)
        animation.fillAfter = true
        animation.repeatCount = Animation.INFINITE
        animation.duration = animateTime
        srcView.startAnimation(animation)
    }

    /**
     * 获取空心圆
     */
    private fun getCircle(strokeColorStr: String) = GradientDrawable().also {
        it.shape = GradientDrawable.OVAL
        val strokeWidth = 2 // 边框宽度
        val strokeColor = Color.parseColor(strokeColorStr) //边框颜色
        it.setStroke(strokeWidth, strokeColor)
    }

}